package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.DeleteConfItemClassReq;
import com.sg.dto.base.component.req.QueryConfItemClassDetailReq;
import com.sg.dto.base.component.req.QueryConfItemClassListReq;
import com.sg.entity.OmsConfItemClass;
import com.sg.service.base.component.MOmsConfItemClassService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsConfItemClassMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsConfItemClassServiceImpl
        extends ServiceImpl<OmsConfItemClassMapper, OmsConfItemClass>
        implements MOmsConfItemClassService {

    @Autowired
    OmsConfItemClassMapper omsConfItemClassMapper;

    /**
     * 2-3-05批量修改配置方案下分组[2047]
     * gen by moon at 10/10/2022, 1:18:46 AM
     */
    @Trace(operationName = "2-3-05批量修改配置方案下分组")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateConfItemClass(List<OmsConfItemClass> omsConfItemClass) {

        if (omsConfItemClass == null || omsConfItemClass.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsConfItemClass);

    }

    /**
     * 2-3-05批量删配置方案下分组[2038]
     * gen by moon at 10/10/2022, 1:18:45 AM
     */
    @Trace(operationName = "2-3-05批量删配置方案下分组")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteConfItemClass(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-3-05查询配置方案下分组列表[1929]
     * gen by moon at 10/10/2022, 1:18:45 AM
     */
    @Trace(operationName = "2-3-05查询配置方案下分组列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfItemClass> queryConfItemClassList(QueryConfItemClassListReq reqDto) {

        try {
            QueryWrapper<OmsConfItemClass> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfItemClass.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfItemClass.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfItemClass.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsConfItemClass>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-05删除配置方案下分组[1927]
     * gen by moon at 10/10/2022, 1:18:44 AM
     */
    @Trace(operationName = "2-3-05删除配置方案下分组")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteConfItemClass(DeleteConfItemClassReq reqDto) {
        reqDto.setSubjectLifeCycle("EDITING");
        try {
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            return super.removeByMap(columnMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-05修改配置方案下分组[1926]
     * gen by moon at 10/10/2022, 1:18:43 AM
     */
    @Trace(operationName = "2-3-05修改配置方案下分组")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateConfItemClass(OmsConfItemClass omsConfItemClass) {
        omsConfItemClass.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfItemClass != null && omsConfItemClass.getSubjectLifeCycle() != null && omsConfItemClass.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfItemClass.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsConfItemClass.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfItemClass != null && omsConfItemClass.getSubjectLifeCycle() != null && omsConfItemClass.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfItemClass.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        return super.updateById(omsConfItemClass);
    }

    /**
     * 2-3-05新增配置方案下分组[1925]
     * gen by moon at 10/10/2022, 1:18:42 AM
     */
    @Trace(operationName = "2-3-05新增配置方案下分组")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addConfItemClass(OmsConfItemClass omsConfItemClass) {
        omsConfItemClass.setConfItemClassId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsConfItemClass.setSubjectLifeCycle("EDITING");
        omsConfItemClass.setIsArchive("FALSE");
        omsConfItemClass.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsConfItemClass.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsConfItemClass.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsConfItemClass.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsConfItemClass.setIsValid("FALSE");
        omsConfItemClass.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsConfItemClass.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsConfItemClass)) {
            return omsConfItemClass.getConfItemClassId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-3-05查询配置方案下分组详情[1928]
     * gen by moon at 10/10/2022, 1:18:44 AM
     */
    @Trace(operationName = "2-3-05查询配置方案下分组详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsConfItemClass queryConfItemClassDetail(QueryConfItemClassDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsConfItemClass> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsConfItemClass> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsConfItemClass retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
